

import 'package:bt_tool/basic/bt_color_tool.dart';
import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_date_tool.dart';
import 'package:bt_tool/basic/bt_language_tool.dart';
import 'package:bt_tool/basic/bt_nav_tool.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/basic/bt_theme.dart';
import 'package:bt_tool/widget/bt_base_btn_widget.dart';
import 'package:bt_tool/widget/bt_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/tools/project/model/project_model.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';

import '../../../common/config/app_router.dart';
import '../../../common/config/app_theme_color_config.dart';
import '../controller/project_main_controller.dart';

class ProjectMainPage extends BTStatefulWidget{
  const ProjectMainPage({super.key,super.routerConfig});
  @override
  State<StatefulWidget> createState() {
    return ProjectMainPageState();
  }

}

class ProjectMainPageState extends BTState<ProjectMainPage>{

  final ProjectMainController controller = ProjectMainController();

  @override
  BTController btCtrl() {
    return controller;
  }

  @override
  Widget build(BuildContext context) {
    Widget widget = Obx((){
      if(controller.dataList.isEmpty){
        return Container(
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: ()=>controller.createClick(),
            child: Text("暂无数据,点击新建".btTrans),
          ),
        );
      }
      return Container(
        padding: const EdgeInsets.all(10),
        child: Column(
          children: [
            Expanded(
              child: MasonryGridView.count(
                crossAxisCount: 2, // 列数
                mainAxisSpacing: 8, // 主轴间距
                crossAxisSpacing: 8, // 交叉轴间距
                itemCount: controller.dataList.length, // 数据总数
                itemBuilder: (context, index) {
                  ProjectModel model = controller.dataList[index];
                  return GestureDetector(
                    onSecondaryTapDown: (TapDownDetails details) => controller.showMenuView(model,details.globalPosition),
                    onTap: ()=>controller.itemClick(index),
                    child: Container(
                      color: AppThemeColor.contentBgColor2,
                      padding: const EdgeInsets.all(5),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(
                            model.name,
                            style: TextStyle(fontSize: 15, color: AppThemeColor.textColor,fontWeight: FontWeight.w600),
                          ),
                          const SizedBox(height: 4,),
                          Text(
                              "创建时间：${BTDateTool.formatter(BTDateTool.YYYYMMDDHHmmss, BTDateTool.fromSeconds(model.createTime))}"
                          ),
                          const SizedBox(height: 4,),
                          GestureDetector(
                            onTap: (){
                              BTRouter.share().pushToUrl(
                                  routerUrl: AppRouterDefine.translateMainPage,
                                  routerParameter: {
                                    "id" : model.id,
                                  }
                              );
                            },
                            child: Text(
                              "去翻译".btTrans,
                              style: TextStyle(
                                color: AppThemeColor.mainColor,
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),
            Row(
              children: [
                BTBtnTextWidget(
                  onTap: ()=>controller.createClick(),
                  bgColor: AppThemeColor.mainColor,
                  constraints: const BoxConstraints.expand(width: 100,height: 40),
                  text: "新增",
                  textColor: Colors.white,
                  alignment: Alignment.center,
                ),
                const SizedBox(width: 10,),
                BTBtnTextWidget(
                  onTap: ()=>controller.getData(),
                  bgColor: AppThemeColor.mainColor,
                  constraints: const BoxConstraints.expand(width: 100,height: 40),
                  text: "刷新",
                  textColor: Colors.white,
                  alignment: Alignment.center,
                ),
              ],
            )
          ],
        ),
      );
    });

    if(controller.isSelectStyle){
      return Scaffold(
        appBar: BTNavTool.createAppBar(title: "选择项目",context: context),
        body: widget,
      );
    }

    return widget;
  }

}
